package com.chidopi.market.aa.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chidopi.cdf.struts1actions.CdfDispatchAction;
import com.chidopi.cdf.util.ModelContextFactory;
import com.chidopi.market.aa.domain.Accounts;
import com.chidopi.market.aa.forms.PasswordForm;
import com.chidopi.market.aa.service.IPasswordService;
import com.chidopi.market.util.MessageHandler;
import com.chidopi.market.util.PasswordEncoder;

public class PasswordAction extends CdfDispatchAction {
	
	private static Logger logger = LoggerFactory.getLogger(PasswordAction.class);
	private IPasswordService  passwordService;
	
	public PasswordAction(){
		this("");
	}
	
	public PasswordAction(String entryURL) {
		this.entryURL = entryURL;
		
		try {
			this.passwordService = (IPasswordService)
				ModelContextFactory.get("passwordService", IPasswordService.class);
		} catch (Exception e) {
			System.err.print(e);
			this.passwordService = null;
			logger.warn("initial", e);
		}
		logger.debug("load passwordService" + passwordService);
	}
	
	public ActionForward pwd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Integer userId = (Integer) request.getSession().getAttribute("user_id");
		logger.debug("userId=" + userId);
		Accounts t = this.passwordService.findById(userId);
		if (null == t) {
			logger.warn("Account[id={}] is deleted!!", userId);
			return mapping.findForward("err");
		}
		PasswordForm passForm = (PasswordForm) form;
		passForm.setOp("chagePwd");
		passForm.setUserId(userId.toString());		         
		return mapping.findForward("success");
	}
	
	public ActionForward changePwd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("changePwd............");
		PasswordForm passForm = (PasswordForm) form;
		logger.info(passForm.getOldPassword() + " | " + passForm.getOldPassword());
		
		
		Integer userId = (Integer) request.getSession().getAttribute("user_id");
		Accounts t = this.passwordService.findById(userId);
		if (null == t) {
			logger.warn("Account[id={}] is deleted!!", userId);
			return mapping.findForward("err");
		}
		
		String password = PasswordEncoder.encode(passForm.getOldPassword(), t.getUsername());

		if(t.getPassword().equals(password) || t.getPassword().equals(passForm.getOldPassword())){
			t.setPassword(passForm.getNewPassword());
			t.setUpdate_by(userId);
			passwordService.changePassword(t);	
		}else{
			logger.debug("error");
			MessageHandler.getInstance().processError(request, "error.login.oldpassword.input");
			return mapping.getInputForward();
		}
		request.setAttribute("entryURL", entryURL);
		return mapping.findForward("success");
	}
}
